/*
 * @Author: wangxiaojie
 * @LastEditors: wangxiaojie
 */
function createHistory() {
  const timeline = {}

  timeline.past = [] //过去状态列表
  timeline.futrue = [] // 为了状态列表
  // timeline.present // 当前状态

  timeline.gotoState = (index) => {
    const allState = [...timeline.past, timeline.present, ...timeline.futrue]
    timeline.present = allState[index]
    timeline.past = allState.slice(0, index)
    timeline.futrue = allState.slice(index + 1, allState.length)
  }
  // 获取当前状态的index
  timeline.getIndex = () => {
    return timeline.past.length
  }

  // 保存当前状态
  timeline.push = (currentState) => {
    if (timeline.present) {
      timeline.past.push(timeline.present)
    }
    timeline.present = currentState
  }

  // 后退
  timeline.undo = () => {
    if(timeline.past.length !== 0) {
      timeline.gotoState(timeline.getIndex() - 1)
    }
  }
  // 前进
  timeline.redo = () => {
    if(timeline.futrue.length !== 0) {
      timeline.gotoState(timeline.getIndex() + 1)
    }
  }

  return timeline

}

module.exports = createHistory